package com.kjsb.controller;

import com.kjsb.service.ProjectManageService;
import com.kjsb.vo.ProjectManage;
import com.kjsb.vo.ProjectSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/projectManage")
public class ProjectManageController {
    @Autowired
    private ProjectManageService service;

    /**
     * 根据pid查询项目信息
     * @param pid 项目id
     * @return Result结果集
     */
    @GetMapping("/{pid}")
    public Result getProject(@PathVariable Integer pid){
        ProjectManage projectManage = service.getByPid(pid);
        Integer code=projectManage != null ? Code.GET_OK : Code.GET_ERR;
        String msg= projectManage != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code,projectManage,msg);
    }

    /**
     * 修改项目信息
     * @param projectManage 修改后信息
     * @return Result结果集
     */
    @PutMapping
    public Result updateProject(@RequestBody ProjectManage projectManage){
        int i = service.updateProject(projectManage);
        Integer code= i== 1 ? Code.UPDATE_OK : Code.UPDATE_ERR;
        String msg= i== 1 ? "修改成功！" : "数据修改失败！";
        return new Result(code,i,msg);
    }

    /**
     * 简单查询
     * @param name 项目名
     * @return Result结果集
     */
    @GetMapping("/simpleSearch/{name}")
    public Result simpleSearch(@PathVariable String name){
        List<ProjectSearch> searches = service.simpleSearch(name);
        Integer code=searches.isEmpty() ? Code.GET_ERR:Code.GET_OK ;
        return new Result(code,searches);
    }
    @GetMapping("/getByCid/{cid}")
    public Result getByCid(@PathVariable Integer cid){
        List<ProjectManage> projectManage = service.getByCid(cid);
        Integer code=projectManage != null ? Code.GET_OK : Code.GET_ERR;
        String msg= projectManage != null ? "查询成功！" : "数据查询失败，请重试！";
        return new Result(code,projectManage,msg);
    }

    /**
     * 新增项目
     */
    @PostMapping
    public Result addProject(@RequestBody ProjectManage projectManage){
        int i=service.addProject(projectManage);
        Integer code=i ==1 ? Code.SAVE_OK : Code.SAVE_ERR;
        String msg= i==1 ? "新增成功！" : "新增失败！";
        return new Result(code,i,msg);
    }
    /**
     * 高级查询
     */
    @PostMapping("/highsearch")
    public Result highSearch(@RequestBody ProjectSearch search){
        List<ProjectSearch> manage=service.highSearch(search);
        Integer code=manage.isEmpty() ?Code.GET_ERR :Code.GET_OK  ;
        String msg= manage.isEmpty() ? "查询失败！":"查询成功！"  ;
        return new Result(code,manage,msg);
    }
}
